package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.MallIntegrationOrderDetailsDtoMapper;
import com.starlink.model.domain.MallIntegrationOrderDetailsDO;
import com.starlink.mapper.MallIntegrationOrderDetailsMapper;
import com.starlink.model.request.MallIntegrationOrderDetailsRequest;
import com.starlink.service.MallIntegrationOrderDetailsService;
import com.starlink.model.util.MallIntegrationOrderDetailsConvert;
import com.starlink.model.vo.MallIntegrationOrderDetailsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.starlink.utils.IDUtils.getId;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class MallIntegrationOrderDetailsServiceImpl extends ServiceImpl<MallIntegrationOrderDetailsMapper, MallIntegrationOrderDetailsDO> implements MallIntegrationOrderDetailsService {
    @Autowired
    private MallIntegrationOrderDetailsMapper mallIntegrationOrderDetailsMapper;

    @Override
    public MallIntegrationOrderDetailsVO getByKey(long id) {
        MallIntegrationOrderDetailsVO mallIntegrationOrderDetailsVO = MallIntegrationOrderDetailsDtoMapper.INSTANCE.do2VO(getById(id));
        return mallIntegrationOrderDetailsVO;
    }

    @Override
    public PageInfo<MallIntegrationOrderDetailsVO> pageQuery(Pageable pageable,MallIntegrationOrderDetailsRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<MallIntegrationOrderDetailsDO> list = mallIntegrationOrderDetailsMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<MallIntegrationOrderDetailsVO> mallIntegrationOrderDetailsVOList = MallIntegrationOrderDetailsConvert.buildMallIntegrationOrderDetailsVoList(list);
        return new PageInfo(mallIntegrationOrderDetailsVOList);
    }

    @Override
    public List<MallIntegrationOrderDetailsVO> listQuery(MallIntegrationOrderDetailsRequest request) {
        List<MallIntegrationOrderDetailsDO> list = mallIntegrationOrderDetailsMapper.selectDOList(MallIntegrationOrderDetailsDtoMapper.INSTANCE.request2DO(request));
        List<MallIntegrationOrderDetailsVO> mallIntegrationOrderDetailsVOS = MallIntegrationOrderDetailsConvert.buildMallIntegrationOrderDetailsVoList(list);
        return mallIntegrationOrderDetailsVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(MallIntegrationOrderDetailsRequest request) {
        request.setId(getId());
        save(MallIntegrationOrderDetailsDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(MallIntegrationOrderDetailsRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(MallIntegrationOrderDetailsDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        mallIntegrationOrderDetailsMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }
}
